<template>
	<view class="wrap">
		<view class="question-list">
			<view class="question-item" v-for="(item, index) in state.items" :key="index"
				@click="onJumpDetail(item.id)">
				<view class="b-title">{{index+1}}. {{item.title}}</view>
				<view class="iconfont icon-more"></view>
			</view>
		</view>
		<!-- 加载中 -->
		<load-more :loadingType="loadingType" :loadingText="loadingText"></load-more>
	</view>
</template>

<script>
	import {
		getQuestionList
	} from '@/service/api/content.js'
	import mixinsCommon from '@/mixins/common.js'
	export default {
		mixins: [mixinsCommon],
		data() {
			return {
				state: {
					items: []
				},
				loadingType: 1,
				loadingText: '',
			}
		},
		onLoad(options) {
			this.loadList(true)
		},
		onReachBottom() {
			if (this.loadingType !== 1 && this.loadingType !== 2) {
				this.loadList();
			}
		},
		methods: {
			loadList(refresh) {
				console.log('loadList', refresh);
				if (refresh) {
					this.page = 1;
					this.state.items = []
				} else {
					this.page++
				}
				this.loadingType = 1
				this.loadingText = ''
				getQuestionList(this.page, 10).then(([
					err, res
				]) => {
					console.log('getList', err, res)
					this.loadingType = -1;
					if (!err) {
						if (res.items.length > 0) {
							this.state.items = [...this.state.items, ...res.items]
						} else {
							this.loadingType = 2
							if (this.page == 1) {
								this.loadingText = '暂无数据'
							}
							this.page-- // 重置分页
						}
					} else {
						this.loadingType = 3
					}
				})
			},
			onJumpDetail(id) {
				uni.navigateTo({
					url: '/pages/content/question_detail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: env(safe-area-inset-bottom);
		background: $pq-bg-color;
	}

	.wrap {}


	.question-list {
		background: #fff;

		.question-item {
			padding: 30upx;
			border-bottom: 1upx solid #eee;
			display: flex;
			align-items: center;
			justify-content: space-between;

			&:last-child {
				border-bottom: none;
			}

			.b-title {
				font-size: 28upx;
				color: #333;
			}
			.b-more {
				font-size: 36upx;
				color: #666;
			}
		}
	}
</style>